package cn.hr.controller;

import cn.hr.pojo.Dictionary;
import cn.hr.pojo.Organization;
import cn.hr.pojo.Post;
import cn.hr.pojo.Recruit;
import cn.hr.service.DictionaryService;
import cn.hr.service.OrganizationService;
import cn.hr.service.PostService;
import cn.hr.service.RecruitService;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 招聘信息控制器
 *
 * @author MO
 */
@Controller
@RequestMapping("/sys/recruit")
public class RecruitController {
    private Logger logger = LoggerFactory.getLogger(RecruitController.class);
    @Resource
    private RecruitService recruitService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private PostService postService;
    @Resource
    private OrganizationService organizationService;


    //发布职位列表查询
    @RequestMapping("/show")
    public String postShow(Model model) {
        List<Dictionary> rtypes = null;
        try {
            rtypes = dictionaryService.selectAllByTypeName("POST_TYPE");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        model.addAttribute("rtypes", rtypes);
        return "recruit/recrList";
    }

    @RequestMapping(value = "/recrList")//, method = RequestMethod.POST)
    public String recrtList(@RequestParam(required = false) Map m, Model model) {
        PageInfo<Map> pageInfo = null;
        Integer pageNo = 1;
        try {
            if (m.get("pageNo") != null) {
                pageNo = Integer.parseInt((String) m.get("pageNo"));

            }
            pageInfo = recruitService.findListByCondition(m, pageNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("pageInfo", pageInfo);
        return "recruit/recrShow";
    }

    //获取职位分类
    @RequestMapping("/getDictionaryData")
    @ResponseBody
    public List<Dictionary> getDictionaryData(@RequestParam("type") String type) {
        List<Dictionary> list = null;
        try {
            list = dictionaryService.selectAllByTypeName(type);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    //职位发布登记
    @RequestMapping("/regShow")
    public String postRegShow(Model model) {
        List<Dictionary> rtypes = null;
        try {
            rtypes = dictionaryService.selectAllByTypeName("RECRUIT_TYPE");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        model.addAttribute("recruit", new Recruit());
        model.addAttribute("recrType", new Dictionary());
        model.addAttribute("organ", new Organization());
        model.addAttribute("post", new Post());
        model.addAttribute("postType", new Dictionary());
        model.addAttribute("attr", "reg");
        model.addAttribute("rtypes", rtypes);
        return "recruit/recrAU";
    }

    @RequestMapping(value = "/regSave", method = RequestMethod.POST)
    public String postRegSave(Recruit recruit, Model model) {
        try {
            if (recruitService.insert(recruit)) {
                return "redirect:/sys/recruit/show";
            }
            model.addAttribute(Constants.SYS_MESSAGE, "添加失败!");
        } catch (Exception e) {
            model.addAttribute(Constants.SYS_MESSAGE, "添加失败!");
            e.printStackTrace();
        }
        return "redirect:/sys/recruit/regShow";
    }

    //修改发布职位登记
    @RequestMapping("/updShow")
    public String postUpdShow(@RequestParam("id") String id,@RequestParam(value = "info",required = false)String info, Model model) {
        Recruit recruit = null;  //招聘广告
        //Dictionary recrType =null;  //招聘分类
        Organization organ = null; //部门
        Post post = null; //职位
        Dictionary postType = null;  //职位分类
        List<Dictionary> rtypes = null;
        try {
            recruit = recruitService.get(Integer.parseInt(id));
            if (recruit != null) {
                //recrType =dictionaryService.get("RECRUIT_TYPE",recruit.getType());
                organ = organizationService.get(recruit.getoId());
                post = postService.get(recruit.getPostId());
                postType = dictionaryService.get("POST_TYPE", post.getSort());
                rtypes = dictionaryService.selectAllByTypeName("RECRUIT_TYPE");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("recruit", recruit);
        //model.addAttribute("recrType", recrType);
        model.addAttribute("rtypes", rtypes);
        model.addAttribute("organ", organ);
        model.addAttribute("post", post);
        model.addAttribute("postType", postType);
        model.addAttribute("attr", "upd");
        if (!StringUtils.isNullOrEmpty(info)){
            return "recruit/recrView";
        }
        return "recruit/recrAU";
    }

    @RequestMapping(value = "/updSave", method = RequestMethod.POST)
    public String postUpdSave(Recruit recruit, Model model) {
        try {
            if (recruitService.update(recruit)) {
                return "redirect:/sys/recruit/show";
            }
        } catch (Exception e) {
            model.addAttribute(Constants.SYS_MESSAGE, "修改失败!");
            e.printStackTrace();
        }
        model.addAttribute(Constants.SYS_MESSAGE, "修改失败!");
        return "redirect:/sys/recruit/updShow?id=" + recruit.getId();
    }


   /* @RequestMapping(value = "/info")
    public String postInfo(@RequestParam("id") String id, Model model) {
        Recruit recruit = null;  //招聘广告
        Organization organ = null; //部门
        Post post = null; //职位
        Dictionary postType = null;  //职位分类
        List<Dictionary> rtypes = null;
        try {
            recruit = recruitService.get(Integer.parseInt(id));
            if (recruit != null) {
                organ = organizationService.get(recruit.getoId());
                post = postService.get(recruit.getPostId());
                postType = dictionaryService.get("POST_TYPE", post.getSort());
                rtypes = dictionaryService.selectAllByTypeName("RECRUIT_TYPE");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("recruit", recruit);
        model.addAttribute("rtypes", rtypes);
        model.addAttribute("organ", organ);
        model.addAttribute("post", post);
        model.addAttribute("postType", postType);
        model.addAttribute("attr", "upd");
        return "recruit/recrView";
    }
*/
    @RequestMapping(value = "/del")
    public String postDel(@RequestParam("id") String id, Model model) {
        if (StringUtils.isNullOrEmpty(id)) {
            model.addAttribute(Constants.SYS_MESSAGE, "删除失败!");
        } else {
            try {
                if (recruitService.delete(Integer.parseInt(id))) {
                    model.addAttribute(Constants.SYS_MESSAGE, "删除成功!");
                } else {
                    model.addAttribute(Constants.SYS_MESSAGE, "删除失败!");
                }
            } catch (Exception e) {
                e.printStackTrace();
                model.addAttribute(Constants.SYS_MESSAGE, "删除失败!");
            }
        }
        return "redirect:/sys/recruit/show";
    }

}
